import { message } from 'antd';
import {
  empListData,
  getBaseInfo,
  getEntryInfo,
  getEmpInsPayInfo,
  getEmpAccuPayInfo,
  getConsultInfo,
  getEmpInsSerInfo,
  getEmpInsRefundInfo,
  getEmpAccuSerInfo,
  getEmpInsCardInfo,
  getEmpDimissionInfo,
  getEmpLaborContInfo,
  getEmpRecordInfo,
  fetchProServiceInfo,
  queryConsultRcrdHis,
} from '@/services/empIntegratedquery';
import { getEmpNameList, recordDic, recordUpdate } from '@/services/searchrecord';

export default {
  state: {
    empList: [],
    empRecordInfo: { list: [] },
    empLaborContInfo: {},
    empDimissionInfo: { list: [] },
    empInsCardInfo: { list: [] },
    empAccuSerInfo: { list: [] },
    empInsRefundInfo: { list: [] },
    empInsSerInfo: { list: [] },
    consultInfo: { list: [] },
    empAccuPayInfo: { list: [] },
    empInsPayInfo: { list: [] },
    entryInfo: { list: [] },
    proServiceInfor: { list: [] },
    baseInfo: {},
    empLabelObj: undefined,
    empNameList: [],
    consultRcrdHis: {},
  },
  effects: {
    *updateDataList({ payload }, { call }) {
      const response = yield call(recordUpdate, payload);
      if (!response.success) {
        message.warning(response.message || '服务端错误');
      }
      return response;
    },
    *getDic({ payload }, { call, put }) {
      const response = yield call(recordDic, payload);
      if (response.success) {
        yield put({
          type: 'updateModalData',
          payload: response.data,
        });
        return response.data;
      }
      return message.warning(response.message);
    },
    *queryConsultRcrdHis({ payload }, { call, put }) {
      const response = yield call(queryConsultRcrdHis, payload);
      if (response.success) {
        yield put({
          type: 'getConsultRcrdHis',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务器端错误');
      }
    },
    *getEmpNameLis({ payload }, { call, put }) {
      const response = yield call(getEmpNameList, payload);
      if (response.success) {
        yield put({
          type: 'updateModalData',
          payload: {
            empNameList: response.data,
          },
        });
      } else {
        message.warning(response.message || '服务器端错误');
      }
    },

    // 获取列表
    *fetchEmpList({ payload }, { call, put }) {
      const response = yield call(empListData, payload);
      if (response.success) {
        if (response.data.length === 0) {
          message.warning('没有查询到该雇员下的信息！');
        } else {
          yield put({
            type: 'empListData',
            payload: response.data,
          });
        }
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取基本信息
    *fetchBaseInfo({ payload }, { call, put }) {
      const response = yield call(getBaseInfo, payload);
      if (response.success) {
        yield put({
          type: 'getBaseInfo',
          payload: response.data || {},
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取产品信息
    *fetchProServiceInfo({ payload }, { call, put }) {
      const response = yield call(fetchProServiceInfo, payload);
      if (response.success) {
        yield put({
          type: 'getProServiceInfo',
          payload: response.data || {},
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取入职信息
    *fetchEntryInfo({ payload }, { call, put }) {
      const response = yield call(getEntryInfo, payload);
      if (response.success) {
        yield put({
          type: 'getEntryInfo',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取社保信息
    *fetchEmpInsPayInfo({ payload }, { call, put }) {
      const response = yield call(getEmpInsPayInfo, payload);
      if (response.success) {
        yield put({
          type: 'getEmpInsPayInfo',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取公积金信息
    *fetchEmpAccuPayInfo({ payload }, { call, put }) {
      const response = yield call(getEmpAccuPayInfo, payload);
      if (response.success) {
        yield put({
          type: 'getEmpAccuPayInfo',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取咨询记录信息
    *fetchConsultInfo({ payload }, { call, put }) {
      const response = yield call(getConsultInfo, payload);
      if (response.success) {
        yield put({
          type: 'getConsultInfo',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取社保信息
    *fetchEmpInsSerInfo({ payload }, { call, put }) {
      const response = yield call(getEmpInsSerInfo, payload);
      if (response.success) {
        yield put({
          type: 'getEmpInsSerInfo',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取返款信息
    *fetchEmpInsRefundInfo({ payload }, { call, put }) {
      const response = yield call(getEmpInsRefundInfo, payload);
      if (response.success) {
        yield put({
          type: 'getEmpInsRefundInfo',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取公积金信息
    *fetchEmpAccuSerInfo({ payload }, { call, put }) {
      const response = yield call(getEmpAccuSerInfo, payload);
      if (response.success) {
        yield put({
          type: 'getEmpAccuSerInfo',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取社保卡信息
    *fetchEmpInsCardInfo({ payload }, { call, put }) {
      const response = yield call(getEmpInsCardInfo, payload);
      if (response.success) {
        yield put({
          type: 'getEmpInsCardInfo',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取离职信息
    *fetchEmpDimissionInfo({ payload }, { call, put }) {
      const response = yield call(getEmpDimissionInfo, payload);
      if (response.success) {
        yield put({
          type: 'getEmpDimissionInfo',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取劳动合同信息
    *fetchEmpLaborContInfo({ payload }, { call, put }) {
      const response = yield call(getEmpLaborContInfo, payload);
      if (response.success) {
        yield put({
          type: 'getEmpLaborContInfo',
          payload: response.data || {},
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
    // 获取咨询信息
    *fetchEmpRecordInfo({ payload }, { call, put }) {
      const response = yield call(getEmpRecordInfo, payload);
      if (response.success) {
        yield put({
          type: 'getEmpRecordInfo',
          payload: { data: response.data },
        });
      } else {
        message.warning(response.message || '获取雇员列表失败：服务器端错误');
      }
    },
  },

  reducers: {
    // 获取列表
    empListData(state, { payload }) {
      return {
        ...state,
        empList: payload,
      };
    },
    // 入职数据
    getEntryInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        entryInfo: { list },
      };
    },
    // 获取社保信息
    getEmpInsPayInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      let pageArr = [];
      if (list.length) {
        pageArr = list.map(item => {
          if (item.empAssList.length) {
            // 基数 empBase+ unitBase
            // 个人比例  empProp
            // 个人缴费  empAmo
            // 公司比例  unitProp
            // 公司缴费  unitAmo
            const obj = {};
            item.empAssList.forEach(c => {
              obj[`empBase_${c.ass}`] = c.empBase || c.empBase === 0 ? c.empBase : '--';
              obj[`unitBase_${c.ass}`] = c.unitBase || c.unitBase === 0 ? c.unitBase : '--';
              obj[`empProp_${c.ass}`] = c.empProp || c.empProp === 0 ? c.empProp : '--';
              obj[`empAmo_${c.ass}`] = c.empAmo || c.empAmo === 0 ? c.empAmo : '--';
              obj[`unitProp_${c.ass}`] = c.unitProp || c.unitProp === 0 ? c.unitProp : '--';
              obj[`unitAmo_${c.ass}`] = c.unitAmo || c.unitAmo === 0 ? c.unitAmo : '--';
              obj[`indvAccoNo_${c.ass}`] = c.indvAccoNo || c.indvAccoNo === 0 ? c.indvAccoNo : '--';
            });
            return {
              ...item,
              ...obj,
            };
          }
          return true;
        });
      }

      return {
        ...state,
        empInsPayInfo: { list: pageArr },
      };
    },
    // 获取公积金信息
    getEmpAccuSerInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];

      return {
        ...state,
        empAccuSerInfo: { list },
      };
    },
    // 获取公积金缴费信息
    getEmpAccuPayInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      let pageArr = [];
      if (list.length) {
        pageArr = list.map(item => {
          if (item.empAssList.length) {
            // 基数 empBase+ unitBase
            // 个人比例  empProp
            // 个人缴费  empAmo
            // 公司比例  unitProp
            // 公司缴费  unitAmo
            const obj = {};
            item.empAssList.forEach(c => {
              obj[`empBase_${c.ass}`] = c.empBase || c.empBase === 0 ? c.empBase : '--';
              obj[`unitBase_${c.ass}`] = c.unitBase || c.unitBase === 0 ? c.unitBase : '--';
              obj[`empProp_${c.ass}`] = c.empProp || c.empProp === 0 ? c.empProp : '--';
              obj[`empAmo_${c.ass}`] = c.empAmo || c.empAmo === 0 ? c.empAmo : '--';
              obj[`unitProp_${c.ass}`] = c.unitProp || c.unitProp === 0 ? c.unitProp : '--';
              obj[`unitAmo_${c.ass}`] = c.unitAmo || c.unitAmo === 0 ? c.unitAmo : '--';
            });
            return {
              ...item,
              ...obj,
            };
          }
          return true;
        });
      }
      return {
        ...state,
        empAccuPayInfo: { list: pageArr },
      };
    },
    // 获取咨询记录
    getConsultInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        consultInfo: { list },
      };
    },
    // 获取社保服务信息
    getEmpInsSerInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        empInsSerInfo: { list },
      };
    },
    // 获取返款信息
    getEmpInsRefundInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        empInsRefundInfo: { list },
      };
    },

    // 获取社保卡信息
    getEmpInsCardInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        empInsCardInfo: { list },
      };
    },
    // 获取离职信息
    getEmpDimissionInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];

      return {
        ...state,
        empDimissionInfo: { list },
      };
    },
    // 获取劳动合同
    getEmpLaborContInfo(state, { payload }) {
      return {
        ...state,
        empLaborContInfo: payload,
      };
    },
    // 获取基本信息
    getBaseInfo(state, { payload }) {
      return {
        ...state,
        baseInfo: payload,
      };
    },
    // 获取产品信息
    getProServiceInfo(state, { payload }) {
      const list = Array.isArray(payload) ? payload : [];
      return {
        ...state,
        proServiceInfor: { list },
      };
    },
    // 获取咨询记录信息
    getEmpRecordInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        empRecordInfo: { list },
      };
    },

    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    getConsultRcrdHis(state, { payload }) {
      const { MSGTYPE } = state;

      let typeNewArr = [];
      let typeKeyArr = [];
      Object.keys(payload).forEach(key => {
        if (key === 'type') {
          // 咨询类型
          const typeArr = payload[key].split(',');
          console.log(payload[key].split(','), 'payload[key].split(', ')');
          typeNewArr = typeArr.map(item => {
            const arrs = MSGTYPE.filter(i => i.itemCode === item);
            return arrs.length ? arrs[0].itemName : null;
          });
          typeNewArr = typeNewArr.join(',');
          typeKeyArr = typeArr;
          console.log(typeArr, 'aa', MSGTYPE);
        }
        console.log(typeKeyArr, 'typeKeyArr');
        // if (key === 'state') {
        //   // 回复状态
        //   const arrs = CONSULT_STATUS.filter(i => Number(i.itemCode) === payload[key]);
        //   statusArr = arrs.length ? arrs[0].itemName : null;
        // }
        // if (key === 'consultWay') {
        //   const arrs = CONSULT_WAY.filter(i => Number(i.itemCode) === payload[key]);
        //   consultWayArr = arrs.length ? arrs[0].itemName : null;
        // }
      });
      return {
        ...state,
        consultRcrdHis: {
          ...payload,
          typeKeyArr,
          typeNewArr,
        },
      };
    },
  },
};
